<?php
require_once 'command/Command.php';
require_once 'database/DatabaseConnection.php';
require_once 'database/Product.php';
require_once 'command/GetTagsCommand.php';

class GetProductsByTagsCommand implements Command{
	
	private $tagCommand;
	private $ids;
	private $product;
	
	public function __construct($ids){
		$this->tagCommand = new GetTagsCommand();
		$this->ids = $this->tagCommand->toArray($ids);
		$this->product = new Product();
	}
	
	public function execute(){
		$string = "<products>";
		DatabaseConnection::connect();
		$arrayObj = $this->product->getProductsByTags($this->ids);
		DatabaseConnection::disconnect();
		$it = $arrayObj->getIterator();
		while($it->valid()){
			$arr = $it->current();
			$string .= "<product>";
			$string .= 	"<id>".$arr['productid']."</id>";
			$string .= 	"<name>".$arr['productname']."</name>";
			$string .= 	"<price>".$arr['productprice']."</price>";
			$string .= 	"<tag>";
			$string .=		"<id>".$arr['tagid']."</id>";
			$string .= 		"<name>".$arr['tagname']."</name>";
			$string .=	"</tag>";
			$string .= "</product>";
			$it->next();
		}
		$string .= "</products>";
		return $string;
	}
}